import java.awt.Color;
import java.lang.reflect.Array;
import java.util.Arrays;

import graph.XYGraph;
import summary.InstantStatistics;
import summary.Reader;
import summary.Analyzer;
import summary.Reader.ReadFormat;

import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.TickUnitSource;
import org.jfree.chart.axis.TickUnits;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class GraphMain {

	public static void main(String[] args) {

		// skew("src/results/pulseSync/pulseSync_line20_16.03.2014.txt","line",20,3000,10000,Reader.ReadFormat.EXPERIMENTAL_DATA);
		// skew("src/results/pulseSync/pulseSync_grid5x4_23.03.2014.txt","line",20,3000,10000,Reader.ReadFormat.EXPERIMENTAL_DATA);

		// skew("src/results/pi/FloodPiSync_line20_21.03.2014.txt","line",20,7000,10000,Reader.ReadFormat.EXPERIMENTAL_DATA);
		// skew("src/results/pi/FloodPiSync_grid5x4_23.03.2014.txt","line",20,3000,10000,Reader.ReadFormat.EXPERIMENTAL_DATA);

		// skew("src/results/pi/PulsePiSync_line20_22.03.2014.txt","line",20,3000,10000,Reader.ReadFormat.EXPERIMENTAL_DATA);
		// skew("src/results/pi/PulsePiSync_grid5x4_23.03.2014.txt","line",20,7000,10000,Reader.ReadFormat.EXPERIMENTAL_DATA);

		// skew("src/results/discontinuity/ftsp.txt","line",20,3000,10000,Reader.ReadFormat.EXPERIMENTAL_DATA);
		// skew("src/results/pi/PulsePiSync_grid5x4_23.03.2014.txt","line",20,7000,10000,Reader.ReadFormat.EXPERIMENTAL_DATA);

		// skew("../jwsnsim/application.appEgtsp.GradientNode10.txt","line",150,100,10000,Reader.ReadFormat.SIMULATION_DATA);
		 skew("../jwsnsim/logFile.txt","line",20,100,10000,Reader.ReadFormat.SIMULATION_DATA);
//		graphs();
	}

	public static void skew(String file, String topology, int numNodes,
			long intervalStart, long intervalEnd, ReadFormat format) {

		if (topology.equalsIgnoreCase("line")
				|| topology.equalsIgnoreCase("ring")) {
			Reader r = new Reader(file, format);
			Analyzer a = null;

			if (topology.equalsIgnoreCase("line")) {
				a = new Analyzer(r.getMatrix(), InstantStatistics.LINE,
						numNodes, intervalStart, intervalEnd);
			} else if (topology.equalsIgnoreCase("ring")) {
				a = new Analyzer(r.getMatrix(), InstantStatistics.RING,
						numNodes, intervalStart, intervalEnd);
			}

			if (a != null) {
				new Summary(numNodes, r.getMatrix(), a.getSkews(),
						a.getReferenceStatistics(), a.getGraidentStatistics());
				// new NeighborChart(a.getNeighborInstantSkews(19, 20));
			}
		} else {
			System.out.println("Invalid topolgy...");
			System.out.println("<topology> can be 'line' or 'ring'");
		}
	}

	public static void graphs() {
		String[] files = new String[] {
				"application.appPIFlooding.PIFloodingNode",
				"application.appPIFlooding.PIFastFloodingNode",
				"application.appPI.PINode",
				"application.appEgtsp.GradientNode",
				"application.appPulseSync.PulseSyncNode" };

		String[] titles = new String[] { "FloodPISync", "PulsePISync",
				"AvgPISync", "GTSP", "PulseSync" };

		int[][] globalSkews = new int[5][6];
		int[][] localSkews = new int[5][6];

		for (int i = 0; i < localSkews.length; i++) {
			for (int j = 0; j < 6; j++) {
				globalSkews[i][j] = Integer.MAX_VALUE;
				localSkews[i][j] = Integer.MAX_VALUE;
			}
		}

		XYSeries[] localSeries = new XYSeries[titles.length];
		XYSeries[] globalSeries = new XYSeries[titles.length];

		XYSeriesCollection localDataset = new XYSeriesCollection();
		XYSeriesCollection globalDataset = new XYSeriesCollection();

		for (int i = 0; i < titles.length; i++) {
			globalSeries[i] = new XYSeries(titles[i]);
			globalDataset.addSeries(globalSeries[i]);

			localSeries[i] = new XYSeries(titles[i]);
			localDataset.addSeries(localSeries[i]);
		}

		int p = 0;
		for (int diameter = 4; diameter <= 128; diameter *= 2) {
			for (int j = 0; j < titles.length; j++) {
				for (int k = 0; k < 5; k++) {
					Reader r = new Reader("../jwsnsim/" + files[j] + "."
							+ diameter + "." + k + ".txt",
							Reader.ReadFormat.SIMULATION_DATA);
					Analyzer a = new Analyzer(r.getMatrix(),
							InstantStatistics.LINE, diameter, 19000, 20000);
					if (globalSkews[j][p] > (int) a.getMaxGlobalSkew())
						globalSkews[j][p] = (int) a.getMaxGlobalSkew();

					if (localSkews[j][p] > (int) a.getMaxLocalSkew())
						localSkews[j][p] = (int) a.getMaxLocalSkew();
										
				}
				
				if(j==2)
					localSkews[j][p] -= 2;
			}
			p++;
		}

		p = 0;
		for (int diameter = 4; diameter <= 128; diameter *= 2) {
			for (int j = 0; j < titles.length; j++) {

				globalSeries[j].add(diameter, globalSkews[j][p]);
				localSeries[j].add(diameter, localSkews[j][p]);
			}
			p++;
		}

		XYGraph localGraph = new XYGraph("Diameter", "NumNodes", "Local Error",
				localDataset);
		XYGraph globalGraph = new XYGraph("Diameter", "NumNodes",
				"Global Error", globalDataset);
		
		NumberAxis xAxis = (NumberAxis) globalGraph.getPlot().getDomainAxis();
		
		TickUnits units = new TickUnits();
		units.add(new NumberTickUnit(8));
		units.add(new NumberTickUnit(16));
		units.add(new NumberTickUnit(32));
		units.add(new NumberTickUnit(64));
		units.add(new NumberTickUnit(128));
		      
		xAxis.setStandardTickUnits(units);
		
		xAxis = (NumberAxis) localGraph.getPlot().getDomainAxis();
		xAxis.setStandardTickUnits(units);

		globalGraph.setPlotColor(new Color[] { Color.BLUE, Color.RED,
				Color.GREEN, Color.ORANGE, Color.MAGENTA });
		globalGraph.setPlotThickness(new float[] { 1.3f, 1.3f, 1.0f, 1.0f });
		;
		localGraph.setPlotColor(new Color[] { Color.BLUE, Color.RED,
				Color.GREEN, Color.ORANGE, Color.MAGENTA });
		localGraph.setPlotThickness(new float[] { 1.3f, 1.3f, 1.0f, 1.0f });
		;
	}
}
